const express = require('express');
const pool = require("../db");
const User = express.Router();

// 用户接口 /user
User.get('/user', (req, res, next) => {
    //获取get传递的参数
    var obj = req.query
    console.log(obj)
    //如果传递的参数为空设置默认值
    //页码为空，默认为第1页
    if (!obj.pno) {
        obj.pno = 1
    }
    //每页数据量为空，默认为为3
    if (!obj.count) {
        obj.count = 5
    }
    console.log(obj, 'objobj')
    //计算开始查询的值
    var start = (obj.pno - 1) * obj.count
    //将每页数据量强制转换为数值
    var size = parseInt(obj.count)
    //执行SQL命令，进行分页查询
    pool.query('select * from user limit ?,?', [start, size], (err, results) => {
        if (err) {
            return next(err)
        }
        console.log(results)
        //结果是数组，把查询的结果响应到前端
        if (results.length === 0) {
            res.send({ code: 400, msg: '暂无领养动物' })
        } else {
            res.send({ code: 200, msg: '查询成功', data: results, total: results.length })

        }
    })

})

User.delete('/de', (req, res, next) => {
    //通过id删除数据
    var obj = req.query;
    console.log(obj);
    pool.query('delete from user where user_id = ?', [obj.user_id], (err, results) => {
        console.log(obj.user_id);
        if (err) {
            //如果SQL中出现错误，交给下一个错误处理中间件
            //阻止往后执行
            return next(err)
        }
        console.log(results, 'results')
        if (results.affectedRows === 0) {
            res.send({ code: 400, msg: '删除失败' })
        } else {
            res.send({ code: 200, msg: '删除成功', data: results })
        }
    })
})
User.post('/add', (req, res, next) => {
    //通过id增加数据
    var obj = req.body;
    console.log(obj);
    pool.query('insert into user  set ?', [obj], (err, results) => {
        if (err) {
            //如果SQL中出现错误，交给下一个错误处理中间件
            //阻止往后执行
            return next(err)
        }
        console.log(results, 'results')
        if (results.affectedRows !== 0) {
            res.send({ code: 200, msg: '增加成功' })
        } else {
            res.send({ code: 400, msg: '增加失败', data: results })
        }
    })
})
// 修改用户信息
// 用户修改
User.put("/put", (req, res, next) => {
    console.log(req.body); if (!req.body.user_id) {
        //obj.uid===
        //响应uid这项不能为空，阻止往后执行
        return res.send({ code: 401, msg: "uid不能为空" })
    }
    pool.query(
        "update user set ? where user_id = ?", [req.body, req.body.user_id], (err, r) => {
            if (err) {
                return next(err);
            }
            console.log(r);
            // 满足条件 则修改成功
            if (r.changedRows === 0) {
                res.send({ code: 400, msg: "修改失败" });
            } else {
                res.send({ code: 200, msg: "修改成功" });
            }
        });
});
// 用户查询(按id查询) 
User.get('/get', (req, res, next) => {
    let obj = req.query.user_id
    console.log(obj);
    pool.query('select * from user where user_id=?', [obj], (err, results) => {
        if (err) {
            //如果SQL中出现错误，交给下一个错误处理中间件
            //阻止往后执行
            return next(err)
        }
        if (results.length === 0) {
            res.send({ code: 500, msg: '暂未查到' })
        } else {
            res.send({ code: 200, msg: '查询成功', data: results })
        }
    })
})
//单个用户查询
User.put('/getxx', (req, res, next) => {
    const name = "%" + req.body.useName + "%"
    pool.query("select * from user where useName like ? ", [name], (err, results) => {
        if (err) {
            //如果SQL中出现错误，交给下一个错误处理中间件
            //阻止往后执行
            return next(err)
        }
        // console.log(results,'results')
        if (results.length === 0) {
            res.send({ code: 400, msg: '暂无领养动物' })
        } else {
            res.send({ code: 200, msg: '查询成功', data: results })
        }
    })
})

module.exports = User